package com.zho.util.rctrl;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

class ServiceManager extends Thread {

	static Collection getServices() {
		return services;
	}

	private static Collection services;

	private boolean stop;

	ServiceManager(Vector services) {
		stop = false;
		ServiceManager.services = services;
	}

	public void run() {
		while (!stop) {
			try {
				rest(10000);
				checkService();
			} catch (InterruptedException e) {
			}
		}
	}

	synchronized void rest(int miliSeconds) throws InterruptedException {
		wait(miliSeconds);
	}

	synchronized void addService(Service service) {
		services.add(service);
	}

	synchronized void checkService() {
		Iterator iter = services.iterator();
		while (iter.hasNext()) {
			Service service = (Service) iter.next();
			if (!service.isAlive()) {
				iter.remove();
			}
		}
	}

	public void stopServiceManager() {
		stop = true;
	}

}
